"""
Created on Mon Jan 18 15:50:27 2021
@author: lzt
"""
"""
Created on Sat Dec 26 11:32:28 2020
@author: lzt
"""

import numpy as np
import os
import pandas as pd
import sys
from sklearn.utils import shuffle

# "python hello.py model a b1 b2 c"

# args = [训练文件绝对路径：String， s1,s2,s3,]
version = sys.argv[-1]
parameters = list(sys.argv[3:-2])
# FPath = "/var/tmp/"
FPath = "./"
prefix = "tujunxiongniubi"
suffix = "menjinsuoniubi"


def mape(y_true, y_pred):
    return np.mean(np.abs((y_pred - y_true) / y_true)) * 100


def main():
    file_name = str(sys.argv[2])
    XX = pd.read_excel(file_name)
    X = pd.read_excel(file_name)

    X = shuffle(X, random_state=0)
    y = X[str(parameters[-1])]

    X['dia'] = X['dia'] * 2
    X['len'] = X['len'] * 2

    droplist = list(set(X.columns.tolist()) - set(parameters[:-1]))
    X = X.drop(droplist, axis=1)

    # 构造训练集、测试集
    # x_train,x_test,y_train,y_test=train_test_split(X,y,random_state=0,train_size=0.9)

    import lightgbm as lgb

    # 测试集
    df_test = pd.concat([X, y], axis=1)
    # 测试列 
    df_test_columns = df_test.columns.tolist()[:-1]
    print('df_test_columns', df_test_columns)

    name = df_test.columns
    df_test.columns = df_test.columns

    # 测试列 cvff-surface-e
    target = df_test[parameters[-1]]
    param = {'num_leaves': 31,
             'min_data_in_leaf': 30,
             'objective': 'regression',
             'max_depth': -1,
             'learning_rate': 0.01,
             "min_child_samples": 20,
             "boosting": "gbdt",
             "feature_fraction": 0.9,
             "bagging_freq": 1,
             "bagging_fraction": 0.9,
             "bagging_seed": 11,
             "metric": 'mae',
             "lambda_l1": 0.1,
             "verbosity": -1,
             "nthread": 8,
             "random_state": 666}
    predictions = np.zeros(len(df_test))

    clf = lgb.Booster(model_file=str(sys.argv[1]))
    predictions = clf.predict(df_test[df_test_columns], num_iteration=clf.best_iteration)
    print('prediction shape', predictions.shape)

    df = pd.DataFrame(predictions)

    predictions
    real_pred = pd.DataFrame({'real': df_test[parameters[-1]], 'pred': predictions})

    df_test['pred-' + sys.argv[-2]] = predictions
    df_test['id'] = XX.loc[XX.index.tolist()]['id']

    result_name = file_name.split("/")[-1][:-5] + "-result-" + version + ".csv"
    result_path = os.path.join(FPath, result_name)
    print(result_path)
    df_test.to_csv(result_path)
    print(prefix + result_path + suffix)


if __name__ == "__main__":
    main()
